/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package databaseHandler;

import Tables.AbstractTable;
import former.AbstractConditionalFormer;
import former.AbstractFormer;
import former.queryFormers.DeleteFormer;
import former.queryFormers.InsertFormer;
import former.queryFormers.UpdateFormer;
import former.queryFormers.selectFormer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author FOE
 */
public class DatabaseHandler 
{
    private ConnectionPool pool;
    private  Database database;
    private AbstractConditionalFormer select ;
    private AbstractConditionalFormer update ;
    private AbstractConditionalFormer delete ;
    private AbstractFormer insert;
    private ClassBinder binder;
    public DatabaseHandler(String driver, String url,String username
            , String password , int connectionsNumber) throws SQLException 
    {
        pool=new ConnectionPool(driver, url, username, password, connectionsNumber, connectionsNumber+10, true);
        database = new Database(pool);
        select = new selectFormer();
        update = new UpdateFormer();
        delete = new DeleteFormer();
        insert = new InsertFormer();
        binder = new ClassBinder();
    }
    
    
    public void test() throws SQLException
    {
        ResultSet excuteQuery = database.excuteQuery("select * from teacher",null);
        database.printResultSet(excuteQuery);
    }
    public boolean insert(AbstractTable object) throws SQLException, Exception
    {
        String query  = insert.formQuery(object);
        if(!query.equals(""))
        {
            List dataList = insert.getDataList();
            Object[] parametersData = new Object[dataList.size()];
            dataList.toArray(parametersData);
            int excuteUpdate = database.excuteUpdate(query, parametersData);
            if(excuteUpdate != -1)
            {
                return true;
            }
        }
        return false;
    }
    
    public <T extends AbstractTable> List<T> select(T object) throws Exception
    {
        String query  = select.formQuery(object);
        ResultSet excuteQuery = database.excuteQuery(query ,null);
        database.printResultSet(excuteQuery);
        return null;
    }
    public boolean update(AbstractTable object) throws SQLException, Exception
    {
        boolean updated = updater(object, update);
        return updated;
    }
    public boolean delete(AbstractTable object) throws SQLException, Exception
    {
        boolean updated = updater(object, delete);
        return updated;
    }
    private boolean updater(AbstractTable object ,AbstractConditionalFormer former) throws SQLException, Exception
    {
        String query  = former.formQuery(object);
        if(!query.equals(""))
        {
            List dataList = former.getDataList();
            Object[] parametersData = new Object[dataList.size()];
            dataList.toArray(parametersData);
            int excuteUpdate = database.excuteUpdate(query, parametersData);
            if(excuteUpdate != -1)
            {
                return true;
            }
        }
        return false;
    }
    private boolean updater(List<AbstractTable> objects ,AbstractConditionalFormer former) throws SQLException, Exception
    {
        boolean done = true;
        List<String> queries = new ArrayList<>();
        List<Object[]> parameters = new ArrayList<>();
        if(objects != null || objects.size()!=0)
        {
            for(AbstractTable object : objects)
            {
                String query  = former.formQuery(object);
                if(!query.equals(""))
                {
                    List dataList = former.getDataList();
                    Object[] parametersData = new Object[dataList.size()];
                    dataList.toArray(parametersData);
                    queries.add(query);
                    parameters.add(parametersData);
                }
                else
                {
                    done = false;
                    break;
                }
            }
            if(done)
            {
                int excuteUpdate = database.excuteUpdate(queries, parameters);
                if(excuteUpdate==-1)
                {
                    done = false;
                }
            }
        }  
        return done;
    }
    
    
    
    
    
}
